﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using _1_31_2015_SLMSite.Data;
using _1_31_2015_SLMSite.Data.Models;
using _1_31_2015_SLMSite.Models;

namespace _1_31_2015_SLMSite.Controllers
{
    public class CommunicationsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: Communications
        public ActionResult Index()
        {
            List<CommunicationViewModel> model = new List<CommunicationViewModel>();
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Communications.Select(c => new CommunicationViewModel 
                {
                     CommunicationTypeId = c.CommunicationTypeId,
                     CommunicationId = c.CommunicationId,
                     CommunicationTypeDesc = c.CommunicationType.CommunicationTypeDesc,
                     Message = c.Message
                }).ToList();
            }

            return View(model);
        }

        // GET: Communications/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Communication communication = db.Communications.Find(id);
            if (communication == null)
            {
                return HttpNotFound();
            }
            return View(communication);
        }

        public List<CommunicationTypeViewModel> HydrateCommunicationTypes()
        {
            List<CommunicationTypeViewModel> model = null;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.CommunicationTypes.Select(t => new CommunicationTypeViewModel
                    {
                        CommunicationTypeDesc = t.CommunicationTypeDesc,
                        CommunicationTypeId = t.CommunicationTypeId
                    }).ToList();
            }
            return model;
        }

        // GET: Communications/Create
        public ActionResult Create()
        {
            CommunicationViewModel model = new CommunicationViewModel();
            model.CommunicationTypes = HydrateCommunicationTypes();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CommunicationViewModel model)
        {
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Communications.Add(new Communication
                {
                    DateCreated = DateTime.Now,
                    DateLastEdited = DateTime.Now,
                    CommunicationTypeId = model.CommunicationTypeId,
                    Message = model.Message
                });
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        //// POST: Communications/Create
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Create([Bind(Include = "CommunicationId,Message,CommunicationTypeId")] Communication communication)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Communications.Add(communication);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.CommunicationTypeId = new SelectList(db.CommunicationTypes, "CommunicationTypeId", "CommunicationTypeDesc", communication.CommunicationTypeId);
        //    return View(communication);
        //}

        // GET: Communications/Edit/5
        public ActionResult Edit(int id=0)
        {

            if (id == 0)
            {
                return RedirectToAction("Index");
            }

            CommunicationViewModel model = new CommunicationViewModel();
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                Communication communication = db.Communications.FirstOrDefault(c => c.CommunicationId == id);
                if(communication == null)
                {
                    return RedirectToAction("Index");
                    
                }
                model.CommunicationId = communication.CommunicationId;
                model.CommunicationTypeDesc = communication.CommunicationType.CommunicationTypeDesc;                
                model.CommunicationTypeId = communication.CommunicationTypeId;
                model.Message = communication.Message;
                model.DateCreated = communication.DateCreated;
                model.DateLastEdited = communication.DateLastEdited;
                model.CommunicationTypes = HydrateCommunicationTypes();
            }
           
            return View(model);
        }

        // POST: Communications/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CommunicationViewModel model, int commId)
        {
            if(commId <= 0)
            {
                return RedirectToAction("Index");
            }
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                Communication communication = db.Communications.FirstOrDefault(c => c.CommunicationId == commId);
                communication.Message = model.Message;
                communication.CommunicationTypeId = model.CommunicationTypeId;
                communication.DateLastEdited = DateTime.Now;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        // When we add scheduling/tracking all of the scheduling/tracking history will need to be removed.
        public ActionResult Delete(int id)
        {
            if (id <= 0)
            {
                return RedirectToAction("Index");
            }
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                Communication communication = db.Communications.Find(id);
                if (communication == null)
                {
                    return RedirectToAction("Index");
                }
                db.Communications.Remove(communication);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

 

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
